#! /usr/bin/python

import math
import time

ttime = time.time()

def generatePrimes(n):
    sieve = range(n)
    for i in range(2, int(math.sqrt(n)+1)):
	for j in range(i**2, n, i):	
	    sieve[j] = 0
    return [p for p in sieve if p != 0]

table = [[0]*10 for i in range(10)]
for i in range(1, 10):
    for j in range(10):
        table[i][(i*j)%10] = j

print table[2][1], table[3][5]

p = generatePrimes(1000010)

def getN(p1, p2):
    result = 0
    digits = []
    while(p1) :
	digits.append(p1%10)
	p1 /= 10

    for i in range(len(digits)):
	col = digits[i] - (p2*result/10**i)%10
	if col < 0: col += 10
	#print table[p2%10][col], col
	result += table[p2%10][col] * 10**i

    return result*p2

print getN(101, 103)

ss = 0
for i in range(2,len(p)-1):
    #print p[i], p[i+1], getN(p[i], p[i+1])
    ss += getN(p[i], p[i+1])

print ss

print p[len(p)-2]
print time.time() - ttime
